package com.ebpm.designer.menu
{
	import com.ebpm.designer.NodeContainer;
	import com.ebpm.designer.WF;
	import com.ebpm.designer.line.Point;
	
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.net.FileReference;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.utils.ByteArray;
	
	import mx.controls.Alert;
	import mx.graphics.codec.JPEGEncoder;
	
	import org.aszip.saving.Method;
	import org.aszip.zip.ASZip;
	
	/**
	 * 文件上传菜单
	 * **/
	public class UploadMenu extends MenuItem
	{
		public function UploadMenu(iconImage:Class, x:Number=0, y:Number=0)
		{
			super(iconImage, x, y);
		}
		
		override public function _click(event:MouseEvent):void
		{
			if(WF.wp.nodeContainers == null || WF.wp.nodeContainers.length == 0) {
				Alert.show("请绘图以后再截图!");
				return ;
			}
			WF.wp.menu.setVisible(false);																																			//隐藏菜单栏
			WF.wp.hideVariablePanels();																																						//隐藏参数面板
			
			var maxPoint:Point = WF.wp.getMaxContainerPoint();
			var bitmapData:BitmapData = new BitmapData(maxPoint.x + 50, maxPoint.y + 50);
			//this表示给整个Application快照 ，你也可以换成pic，给图片的容器拍照，得到的是图片的照片保存。
			bitmapData.draw(WF.wp,new Matrix());
			var jpg:JPEGEncoder = new JPEGEncoder();
			var ba:ByteArray = jpg.encode(bitmapData);
			
			/** 引入第三方 ZIP 生存包 **/
			var zip:ASZip = new ASZip();
			zip.addFile(ba, "flow.jpg");																																					//添加截图
			var bytes:ByteArray = new ByteArray();
			bytes.writeUTFBytes(SaveMenu.buildXmlData());
			zip.addFile(bytes, "flow.xml");																																			//添加xml文件
			var zipData:ByteArray = zip.saveZIP(Method.LOCAL);
			var file:FileReference = new FileReference();
			file.save(zipData, "flow.zip");
			
//			文件打包上传
//			var url:URLRequest = new URLRequest("http://localhost:8080/FileUpload/FileUpload?aaa=bbbb");
//			url.contentType = "application/octet-stream";
//			url.data = ToByteArray(SaveMenu.initFile(), ba);
//			url.method = URLRequestMethod.POST;
//			
//			var urlLoader:URLLoader = new URLLoader();
//			urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
//			urlLoader.addEventListener(Event.COMPLETE, aaa);
//			urlLoader.load(url);
//			file.save(ba,"pic.jpg");
			
			WF.wp.menu.setVisible(true);
		}
		
		private function convertStringToByteArray(string:String):ByteArray
		{
			var bytes:ByteArray;
			if ( string) {
				bytes = new ByteArray();
				bytes.writeUTFBytes(string);// writeUTFBytes(value:String) 将 UTF-8 字符串写入字节流;
			}
			return bytes;
		}
		
		private function ToByteArray(string:String, bs:ByteArray):ByteArray
		{
			var bytes:ByteArray;
			if ( string) {
				bytes = new ByteArray();
				bytes.writeUTFBytes(string);// writeUTFBytes(value:String) 将 UTF-8 字符串写入字节流;
				Alert.show("" + bytes.length);
			}
			
			var length:Number = bytes.length + bs.length;
			var data:ByteArray = new ByteArray();
			data.writeInt(bytes.length);
			data.writeBytes(bytes, 0, bytes.length);
			data.writeBytes(bs, 0, bs.length);
//			Alert.show("bbbb");
			
			return data;
		}
		
		
		
		
		
		
		
		
		
		
		
		
		
		
	}
}